﻿using System;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace EbbinghausWord.BLL
{
    /// <summary>
    ///  用户配置管理类
    /// </summary>
    public class UserConfig
    {
        #region private field

        private static IsolatedStorageSettings _settings = IsolatedStorageSettings.ApplicationSettings;
        #endregion

        #region public property

        /// <summary>
        /// 是否自动播放声音，默认是允许,true
        /// </summary>
        public bool IsAutoPlayVoice
        {
            set
            {
                CreateOrEditSetting("IsAutoPlayVoice", value);
            }

            get
            {
                return GetBoolFromSettingDefault<bool>("IsAutoPlayVoice", false);
            }
        }

        /// <summary>
        /// 是否显示词组
        /// </summary>
        public bool IsShowWordGroup
        {
            set
            {
                CreateOrEditSetting("IsShowWordGroup", value);
            }

            get
            {
                return GetBoolFromSettingDefault("IsShowWordGroup");
            }
        }

        /// <summary>
        /// 是否显示同义词
        /// </summary>
        public bool IsShowSynonym
        {
            set
            {
                CreateOrEditSetting("IsShowSynonym", value);
            }

            get
            {
                return GetBoolFromSettingDefault("IsShowSynonym");
            }
        }

        /// <summary>
        /// 是否显示例句
        /// </summary>
        public bool IsShowSentence
        {
            set
            {
                CreateOrEditSetting("IsShowSentence", value);
            }

            get
            {
                return GetBoolFromSettingDefault("IsShowSentence");
            }
        }

        /// <summary>
        /// 是否第一次打开
        /// </summary>
        public bool IsFirstOpen
        {
            set
            {
                CreateOrEditSetting("IsFirstOpen", value);
            }

            get
            {
                var tmp = GetBoolFromSettingDefault("IsFirstOpen");
                return tmp;
            }
        }

        #endregion

        #region private method

        /// <summary>
        /// 返回独立存储设置的值，如果不存在，则返回默认值true
        /// </summary>
        /// <returns></returns>
        private static bool GetBoolFromSettingDefault(string key)
        {
            bool isAutoPlayVoice;
            return !_settings.TryGetValue(key, out isAutoPlayVoice) || isAutoPlayVoice;
        }

        /// <summary>
        /// 返回独立存储设置的值，如果不存在，则返回默认值
        /// </summary>
        /// <returns></returns>
        private static T GetBoolFromSettingDefault<T>(string key, T t)
        {
            T getValue;
            return !_settings.TryGetValue(key, out getValue) ? t : getValue;
        }

        /// <summary>
        /// 给独立存储的设置赋值，如果不存在该设置则创建一个
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        private static void CreateOrEditSetting(string key, object value)
        {
            if (!_settings.Contains(key))
            {
                _settings.Add(key, value);
            }
            else
            {
                _settings[key] = value;
            }
        }
        #endregion
    }
}
